﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTOQLDV;
using System.Data;
using System.Data.OleDb;

namespace DAOQLDV
{
    public class DAODichVu
    {
        public static List<DTODichVu> LayDanhSachDichVu()
        {
            List<DTODichVu> dsDichVu = new List<DTODichVu>();
            DataAccess da = new DataAccess();
            try
            {
                da.Open();
                String sql = "Select * From DichVu";
                OleDbDataReader dr = da.ExecuteQuery(sql);
                while (dr.Read())
                {
                    DTODichVu dtoDV = new DTODichVu();
                    dtoDV.MaDichVu = (int)dr["MaDichVu"];
                    dtoDV.TenDichVu = (String)dr["TenDichVu"];
                    dsDichVu.Add(dtoDV);
                }
                dr.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                da.Close();
            }
            return dsDichVu;
        }
        public static DTODichVu LayThongTinDichVu(String tenDichVu)
        {
            DTODichVu dtoDichVu = new DTODichVu();
            DataAccess da = new DataAccess();
            try
            {
                da.Open();
                String sql = "Select * From DichVu Where TenDichVu Like '"+tenDichVu+"'";
                OleDbDataReader dr = da.ExecuteQuery(sql);
                if (dr.Read())
                {
                    dtoDichVu.MaDichVu = (int)dr["MaDichVu"];
                    dtoDichVu.TenDichVu = dr["TenDichVu"].ToString();
                }
                dr.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                da.Close();
            }
            return dtoDichVu;
        }        
        public static bool ThemDichVu(DTODichVu dtoDV)
        {
            bool kq = false;
            DataAccess da = new DataAccess();
            try
            {
                da.Open();
                da.BeginTran();
                String sql = "Insert Into TenDichVu(TenDichVu) Values ('" + dtoDV.TenDichVu + "')";
                da.ExecuteNonQuery(sql);
                sql = "Select @@IDENTITY";
                dtoDV.MaDichVu = (int)da.ExecuteScalar(sql);
                da.CommitTran();
                kq = true;
            }
            catch (Exception ex)
            {
                da.RollBackTran();
                throw ex;
            }
            finally
            {
                da.Close();
            }
            return kq;
        }        
        public static bool CapNhatDichVu(DTODichVu dtoDV)
        {
            bool kq = false;
            DataAccess da = new DataAccess();
            try
            {
                da.Open();
                String sql = "Update TenDichVu Set TenDichVu = '" + dtoDV.TenDichVu + "' Where MaTenDichVu = " + dtoDV.MaDichVu;
                da.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                da.Close();
            }
            return kq;
        }
        public static bool XoaDichVu(DTODichVu dtoDV)
        {
            bool kq = false;
            DataAccess da = new DataAccess();
            try
            {
                da.Open();
                String sql = "Delete From TenDichVu Where MaTenDichVu = " + dtoDV.MaDichVu;
                da.ExecuteNonQuery(sql);
                kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                da.Close();
            }
            return kq;
        }
        public static int LayMaDichVu(String tenDichVu)
        {
            int kq = 0;
            DataAccess da = new DataAccess();
            try
            {
                da.Open();
                String sql = "Select MaDichVu From DichVu Where TenDichVu Like '" + tenDichVu + "'";
                kq = int.Parse("0" + da.ExecuteScalar(sql));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                da.Close();
            }
            return kq;
        }
    }
}
